import 'package:flutter/material.dart';
import 'package:mediapipe_task_vision_example/page/p2p_connect.dart';

class KeyTest extends StatefulWidget {
  final ip;

  const KeyTest({super.key, this.ip});

  @override
  State<KeyTest> createState() => _KeyTestState();
}

class _KeyTestState extends State<KeyTest> {
  P2PConnection? _socket;

  @override
  void initState() {
    _socket = P2PConnection(widget.ip)..connect();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(),
        body: Container(
            child:
            Column(children:[
            Row(children: [
          MaterialButton(
              onPressed: () {
                _sendMsg(37);
              },
              child: Text("LEFT")),

          MaterialButton(
              onPressed: () {
                _sendMsg(40);
              },
              child: Text("DOWN")),
              MaterialButton(
                  onPressed: () {
                    _sendMsg(38);
                  },
                  child: Text("UP")),
          MaterialButton(
              onPressed: () {
                _sendMsg(39);
              },
              child: Text("RIGHT")),
        ]),
              MaterialButton(
                  onPressed: () {
                    _sendMsg(10);
                  },
                  child: Text("ENTER")),


            ])));
  }

  @override
  void deactivate() {
    _socket!.close();
    super.deactivate();
  }

  void _sendMsg(int keyCode) {
    var map = {"openType": "keydown", "keyCode": keyCode};
    _socket!.send(map);
   map = {"openType": "keyup", "keyCode": keyCode};
    _socket!.send(map);
  }
}
